<?php


$sql = "SELECT module, status FROM `".TABLE_MODULES_SCHEDULED."`";

$query = $db->execute($sql);

$scheduled = array();

while(!$query->EOF){
  $scheduled[$query->fields['module']] = $query->fields['status'] ;
  $query->MoveNext();
}

?>

<h2><?php echo TEXT_OPTIONAL_HEADING ;?></h2>

<?php echo TEXT_OPTIONAL_LANGUAGE_NOTES ; ?>

<table class="su_admin_table">
  <tr>
  <th><?php echo TABLE_HEADING_MODULE ; ?></th>
  <th><?php echo TABLE_HEADING_VERSION ; ?></th>
  <th><?php echo TABLE_HEADING_DESCRIPTION ; ?></th>
  <th><?php echo TABLE_HEADING_LICENCE ; ?></th>
  <th><?php echo TABLE_HEADING_AUTHOR ; ?></th>
  <th><?php echo TABLE_HEADING_NOTES ; ?></th>
  <th><?php echo TABLE_HEADING_TICKINSTALL ; ?></th>
  </tr>
<?php
$sql = "SELECT * FROM `".TABLE_MODULES_OPTIONAL."`";

$query = $db->execute($sql);


while(!$query->EOF){
// print_r($query->fields);

$reinstall = '<a href="javascript: void(0);" onclick="$(this).next(\'div .hddn\').show()">'.TEXT_OPTIONAL_REINSTALL.'</a><div class="hddn"><form action="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=optional').'" method="post"><input type="hidden" name="reinstall" value="1" /><input type="hidden" name="module" value="'.$query->fields['module'].'"/><input type="hidden" name="version" value="'.$query->fields['version'].'" /><input type="checkbox" onclick="this.form.submit()" /></form></div>' ;

// $reinstall = '';

if(in_array($query->fields['module'], array_keys($scheduled) ) ){
  switch($scheduled[$query->fields['module']]){
    case 'I':
     $status = TEXT_OPTIONAL_STATUS_INSTALLED.$reinstall;
    break;
    case 'E':
     $status = TEXT_OPTIONAL_STATUS_ERROR.$reinstall;
    break;
    case 'Z':
     $status = TEXT_OPTIONAL_STATUS_UNZIPPED;
    break;
    case 'V':
     $status = TEXT_OPTIONAL_STATUS_VERIFIED;
    break;
    case 'D':
     $status = TEXT_OPTIONAL_STATUS_DOWNLOADING;
    break;
    case 'C':
     $status = TEXT_OPTIONAL_STATUS_CORRUPTED;
    break;
    case 'F':
     $status = TEXT_OPTIONAL_STATUS_FAILED;
    break;
    default:
     $status = TEXT_OPTIONAL_STATUS_SCHEDULED;
  }
}else{
  $status = '<form action="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=optional').'" method="post"><input type="hidden" name="module" value="'.$query->fields['module'].'"/><input type="hidden" name="version" value="'.$query->fields['version'].'" /><input type="checkbox" onclick="this.form.submit()" /></form>' ;
}

print '<tr><td>'.$query->fields['module'].'</td><td class="cntr">'.$query->fields['version'].'</td><td>'.$query->fields['description'].'</td><td>'.$query->fields['licence'].'</td><td>'.$query->fields['authors'].'</td><td>'.$query->fields['notes'].'</td><td>'.  $status .'</td></tr>';
$query->MoveNext();
}
?>
</table>

<h2>Optional updates.</h2>

<table class="su_admin_table">
  <tr>
  <th><?php echo TABLE_HEADING_MODULE ; ?></th>
  <th><?php echo TABLE_HEADING_VERSION ; ?></th>
  <th><?php echo TABLE_HEADING_STATUS ; ?></th>
  </tr>
<?php
$sql = "SELECT aua.module, aua.version, aua.status, URL, last_checked FROM `".TABLE_AUTO_UPDATE_AVAILABLE."` aua LEFT JOIN `".TABLE_AUTO_UPDATE."` au ON aua.module=au.module WHERE status !='F' ";

$query = $db->execute($sql);
$error_recovery = array();

while(!$query->EOF){
// print_r($query->fields);
  print '<tr><td>'.$query->fields['module'].'</td><td class="cntr">'.$query->fields['version'].'</td><td>'.( ($query->fields['status']=='E') ? 'Error, see error log below' : (($conf['autodownload']==1) ? 'Download scheduled:'.date('H:i:s d-M-Y', $query->fields['last_checked']) : 'Auto download disabled<br /> Update location: <a href="'. $query->fields['URL'] .'">'.$query->fields['URL'].'</a>')).'</td></tr>';

  if($query->fields['status']=='E'){
    $error_recovery[] = 'Clear <a href="'.zen_href_link(FILENAME_AUTO_UPDATE, 'm='.urlencode($query->fields['module']) ).'">auto update for '.$query->fields['module'].'</a>';
  }

$query->MoveNext();
}

?>
</table>

